package com.liusy.code.templates;

import com.liusy.code.bean.*;

public class ActionTemplate
{
  protected static String nl;
  public static synchronized ActionTemplate create(String lineSeparator)
  {
    nl = lineSeparator;
    ActionTemplate result = new ActionTemplate();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "package ";
  protected final String TEXT_2 = ";" + NL + "" + NL + "import java.util.Date;" + NL + "import java.util.HashMap;" + NL + "import java.util.Map;" + NL + "" + NL + "import org.apache.shiro.SecurityUtils;" + NL + "import org.apache.struts2.convention.annotation.Action;" + NL + "import org.apache.struts2.convention.annotation.Namespace;" + NL + "import org.apache.struts2.convention.annotation.ParentPackage;" + NL + "import org.apache.struts2.convention.annotation.Result;" + NL + "import org.apache.struts2.convention.annotation.Results;" + NL + "import org.springframework.beans.factory.annotation.Autowired;" + NL + "import org.springframework.beans.factory.annotation.Qualifier;" + NL + "import org.springframework.context.annotation.Scope;" + NL + "import org.springframework.stereotype.Controller;" + NL + "" + NL + "import com.gdtopway.framework.right.constants.LoginConstants;" + NL + "import com.gdtopway.framework.right.dao.IAccountDAO;" + NL + "import com.gdtopway.framework.right.domain.Account;" + NL + "import com.gdtopway.framework.web.action.JsonBaseAction;" + NL + "import com.gdtopway.zhaobiao.util.DateUtility;" + NL + "import com.gdtopway.zhaobiao.util.PageContent;" + NL + "import com.gdtopway.zhaobiao.util.PageRequest;" + NL + "import ";
  protected final String TEXT_3 = ".";
  protected final String TEXT_4 = ";" + NL + "import ";
  protected final String TEXT_5 = ".";
  protected final String TEXT_6 = ";" + NL + "" + NL + "@Controller" + NL + "@Scope(\"prototype\")" + NL + "@Namespace(\"/web/";
  protected final String TEXT_7 = "/";
  protected final String TEXT_8 = "\")" + NL + "@ParentPackage(\"json-default\")" + NL + "@Results({" + NL + "\t\t@Result(name = \"json\", type = \"json\", params = { \"root\", \"dataJsonMap\" })," + NL + "\t\t@Result(name = \"";
  protected final String TEXT_9 = "Add\", location = \"/web/";
  protected final String TEXT_10 = "/";
  protected final String TEXT_11 = "_add.jsp\") })" + NL + "public class ";
  protected final String TEXT_12 = "Action extends JsonBaseAction {" + NL + "" + NL + "" + NL + "\t@Autowired" + NL + "\t@Qualifier(\"";
  protected final String TEXT_13 = "\")" + NL + "\tprivate ";
  protected final String TEXT_14 = " ";
  protected final String TEXT_15 = ";" + NL + "" + NL + "\tprivate ";
  protected final String TEXT_16 = " ";
  protected final String TEXT_17 = ";" + NL + "\t" + NL + "\t\t" + NL + "\tprivate String type; // 类型" + NL + "\t" + NL + "\tprivate String ids; // 类型" + NL + "\t" + NL + "\t" + NL + "\tpublic String getType() {" + NL + "\t\treturn type;" + NL + "\t}" + NL + "" + NL + "" + NL + "\tpublic void setType(String type) {" + NL + "\t\tthis.type = type;" + NL + "\t}" + NL + "\t" + NL + "\tpublic String getIds() {" + NL + "\t\treturn ids;" + NL + "\t}" + NL + "" + NL + "\tpublic void setIds(String ids) {" + NL + "\t\tthis.ids = ids;" + NL + "\t}" + NL + "\t" + NL + "\tpublic ";
  protected final String TEXT_18 = " get";
  protected final String TEXT_19 = "() {" + NL + "\t\treturn ";
  protected final String TEXT_20 = ";" + NL + "\t}" + NL + "" + NL + "\tpublic void set";
  protected final String TEXT_21 = "(";
  protected final String TEXT_22 = " ";
  protected final String TEXT_23 = ") {" + NL + "\t\tthis.";
  protected final String TEXT_24 = " = ";
  protected final String TEXT_25 = ";" + NL + "\t}" + NL + "\t" + NL + "\t" + NL + "\t" + NL + "\t/**" + NL + "\t * 查询" + NL + "\t * @return" + NL + "\t */" + NL + "\t@Action(\"find";
  protected final String TEXT_26 = "List\")" + NL + "\tpublic String find";
  protected final String TEXT_27 = "List() {" + NL + "\t\ttry {" + NL + "\t\t\tMap<String,String> parmsMap=this.getRecord();" + NL + "\t\t\tPageRequest pageRequest = new PageRequest(this.rows, this.page - 1,this.sidx, this.sord);" + NL + "\t\t\tPageContent pageContent = this.";
  protected final String TEXT_28 = ".find";
  protected final String TEXT_29 = "List(pageRequest,parmsMap);" + NL + "\t\t\tthis.fillData4PaginGrid(pageContent.getContent(),pageContent.getRowCount());" + NL + "\t\t} catch (Exception e) {" + NL + "\t\t\te.printStackTrace();" + NL + "\t\t}" + NL + "\t\treturn JSON;" + NL + "\t}" + NL + "\t" + NL + "\t" + NL + "\t@Action(\"";
  protected final String TEXT_30 = "Add\")" + NL + "\tpublic String ";
  protected final String TEXT_31 = "Add() {" + NL + "\t\ttry {" + NL + "\t\t\tif (null != ";
  protected final String TEXT_32 = " && null != ";
  protected final String TEXT_33 = ".getId()) {" + NL + "\t\t\t\t";
  protected final String TEXT_34 = " = this.";
  protected final String TEXT_35 = ".get(";
  protected final String TEXT_36 = ".getId());" + NL + "\t\t\t}" + NL + "\t\t\treturn \"";
  protected final String TEXT_37 = "Add\";" + NL + "\t\t} catch (Exception e) {" + NL + "\t\t\te.printStackTrace();" + NL + "\t\t}" + NL + "\t\treturn null;" + NL + "\t}" + NL + "" + NL + "" + NL + "\t/**" + NL + "\t * 保存" + NL + "\t * " + NL + "\t * @return" + NL + "\t */" + NL + "\t@Action(\"save";
  protected final String TEXT_38 = "\")" + NL + "\tpublic String save";
  protected final String TEXT_39 = "() { " + NL + "\t\tString msg = \"fail\";" + NL + "\t\ttry {" + NL + "\t\t\t";
  protected final String TEXT_40 = " = this.";
  protected final String TEXT_41 = ".save(";
  protected final String TEXT_42 = ");" + NL + "\t\t\tmsg = \"success\";" + NL + "\t\t} catch (Exception e) {" + NL + "\t\t\te.printStackTrace();" + NL + "\t\t}" + NL + "\t\tthis.createJsonMap().putValue(\"msg\", msg);" + NL + "\t\treturn JSON;" + NL + "\t}" + NL + "\t" + NL + "" + NL + "\t/**" + NL + "\t * 删除" + NL + "\t */" + NL + "\t@Action(\"delete";
  protected final String TEXT_43 = "\")" + NL + "\tpublic String delete";
  protected final String TEXT_44 = "() {" + NL + "\t\tString msg = \"fail\";" + NL + "\t\ttry {" + NL + "\t\t\tString[] idStrings = ids.split(\",\");" + NL + "\t\t\tthis.";
  protected final String TEXT_45 = ".delete(idStrings[0]);" + NL + "\t\t\tmsg = \"success\";" + NL + "\t\t} catch (Exception e) {" + NL + "\t\t\te.printStackTrace();" + NL + "\t\t}" + NL + "\t\tthis.createJsonMap().putValue(\"msg\", msg);" + NL + "\t\treturn JSON;" + NL + "\t}" + NL + "\t" + NL + "\t" + NL + "\t/**" + NL + "\t * 保存" + NL + "\t * " + NL + "\t * @return" + NL + "\t */" + NL + "\t@Action(\"update";
  protected final String TEXT_46 = "\")" + NL + "\tpublic String update";
  protected final String TEXT_47 = "() { " + NL + "\t\tString msg = \"fail\";" + NL + "\t\ttry {" + NL + "\t\t\t";
  protected final String TEXT_48 = " = this.";
  protected final String TEXT_49 = ".update(";
  protected final String TEXT_50 = ");" + NL + "\t\t\tmsg = \"success\";" + NL + "\t\t} catch (Exception e) {" + NL + "\t\t\te.printStackTrace();" + NL + "\t\t}" + NL + "\t\tthis.createJsonMap().putValue(\"msg\", msg);" + NL + "\t\treturn JSON;" + NL + "\t}" + NL + "\t" + NL + "\t" + NL + "" + NL + "}" + NL;
  protected final String TEXT_51 = NL;

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    
	JavaTemplateArgs args = (JavaTemplateArgs)argument;
	
	String modeName = args.getModeName();
	String packageName = args.getPackageName();
	String className = args.getClassName();
	
	String entityPackageName = args.getPackageNameInfo().getEntityPackageName();
	String daoPackageName = args.getPackageNameInfo().getDaoPackageName();
	String servicePackageName = args.getPackageNameInfo().getServicPackageName();
	String actionPackageName = args.getPackageNameInfo().getActionPackageName();
	
	String entityClassName = args.getClassNameInfo().getEntityClassName();
	String daoClassName = args.getClassNameInfo().getDaoClassName();
	String serviceClassName = args.getClassNameInfo().getServicClassName();
	String actionClassName = args.getClassNameInfo().getActionClassName();
	
	String entityObjectName = args.getClassNameInfo().getEntityObjectName();
	String daoObjectName = args.getClassNameInfo().getDaoObjectName();
	String serviceObjectName = args.getClassNameInfo().getServicObjectName();
	String actionObjectName = args.getClassNameInfo().getActionObjectName();

    stringBuffer.append(TEXT_1);
    stringBuffer.append(packageName);
    stringBuffer.append(TEXT_2);
    stringBuffer.append(entityPackageName);
    stringBuffer.append(TEXT_3);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_4);
    stringBuffer.append(servicePackageName);
    stringBuffer.append(TEXT_5);
    stringBuffer.append(serviceClassName);
    stringBuffer.append(TEXT_6);
    stringBuffer.append(modeName);
    stringBuffer.append(TEXT_7);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_8);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_9);
    stringBuffer.append(modeName);
    stringBuffer.append(TEXT_10);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_11);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_12);
    stringBuffer.append(args.getClassNameInfo().getServicObjectName());
    stringBuffer.append(TEXT_13);
    stringBuffer.append(args.getClassNameInfo().getServicClassName());
    stringBuffer.append(TEXT_14);
    stringBuffer.append(args.getClassNameInfo().getServicObjectName());
    stringBuffer.append(TEXT_15);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_16);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_17);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_18);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_19);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_20);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_21);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_22);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_23);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_24);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_25);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_26);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_27);
    stringBuffer.append(args.getClassNameInfo().getServicObjectName());
    stringBuffer.append(TEXT_28);
    stringBuffer.append(args.getClassNameInfo().getEntityClassName());
    stringBuffer.append(TEXT_29);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_30);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_31);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_32);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_33);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_34);
    stringBuffer.append(args.getClassNameInfo().getServicObjectName());
    stringBuffer.append(TEXT_35);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_36);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_37);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_38);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_39);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_40);
    stringBuffer.append(args.getClassNameInfo().getServicObjectName());
    stringBuffer.append(TEXT_41);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_42);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_43);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_44);
    stringBuffer.append(args.getClassNameInfo().getServicObjectName());
    stringBuffer.append(TEXT_45);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_46);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_47);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_48);
    stringBuffer.append(args.getClassNameInfo().getServicObjectName());
    stringBuffer.append(TEXT_49);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_50);
    stringBuffer.append(TEXT_51);
    return stringBuffer.toString();
  }
}
